package cn.cjyll.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.cjyll.C3P0DataSoure.JDBCUtilsC3P0;

public class adminLoginServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String username = request.getParameter("user");
		String password = request.getParameter("pass");
//		System.out.println("�˺����룺" + username+password + "����·����" );
		if(username!=""&&password!="") {
		Connection con = null;
		
		try {
		con = JDBCUtilsC3P0.getConnection();
		String sql = "select password from admin where username = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, username);	
		ResultSet res = ps.executeQuery();
		if(res.next())
		{	
//			request.getRequestDispatcher("/management/admin/index.jsp").forward(request, response); 		//  ��½�ɹ���ת
			if(res.getString("password").equals(password.trim()))
			{
				//  ����session��
				request.getSession().setAttribute("password", password);
				request.getSession().setAttribute("username", username);
				request.getSession().setAttribute("adminislogin", " ");
				// ����COOKIE
				 Cookie cookie = new Cookie("adminautoLogin", username + "-" + password);
				 cookie.setMaxAge(5);
				 cookie.setPath(request.getContextPath());
				 response.addCookie(cookie);
//				 System.out.println("cookie������");
				System.out.println(request.getSession().getAttribute("adminislogin"));
				request.getRequestDispatcher( "/management/admin/index.jsp").forward(request, response);
			}else {
				request.setAttribute("errorMsg", "*��½ʧ�ܣ������û���������");
				request.getRequestDispatcher( "/management/admin/login.jsp").forward(request, response);
			}
		}else {
			request.setAttribute("errorMsg", "*��½ʧ�ܣ������û���������");
			request.getRequestDispatcher( "/management/admin/login.jsp").forward(request, response);
		}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}else {
			request.setAttribute("errorMsg", "*�û��������벻��Ϊ��");
			request.getRequestDispatcher( "/management/admin/login.jsp").forward(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
